using System;
using System.Windows.Forms;
using usm.SolDev.Common.Config;

namespace usm.SolDev.Common
{
    public class SolDevException : SystemException
    {
        
        #region Constructors    
        /// <summary>
        ///     Default constructor 
        /// </summary>
        public SolDevException() {}
        
        /// <summary>
        ///     Throw an exception and display the specified message
        /// </summary>
        /// <param name="message">Message to show to user</param>
        public SolDevException(string message) : this ()
        {
            SolDevLogger.Error(message);
            MessageBox.Show(message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        /// <summary>
        ///     Throw an exception with a message post placed
        /// </summary>
        /// <param name="message"></param>
        /// <param name="ex"></param>
        public SolDevException(string message, Exception ex) : this ()
        {
            SolDevLogger.Error("{0} : {1}", message, ex);
            MessageBox.Show(String.Format("{0}\nDetailed exception message:\n{1}", message, ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        public SolDevException(SolDevExceptionKey key) : this ()
        {
            SolDevLogger.Error("{0}", key);
            MessageBox.Show(key.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        public SolDevException(SolDevExceptionKey key, Exception ex) : this ()
        {
            SolDevLogger.Error("{0} : {1}", key, ex);
            MessageBox.Show(String.Format("{0}\n Detailed exception message:\n{1} ", key.ToString(), ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="message"></param>
        public SolDevException(SolDevExceptionKey key, String message) : this ()
        {
            SolDevLogger.Error("{0} : {1}", key.ToString(), message);
            MessageBox.Show(message, key.ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        /// <summary>
        ///     Display a formated message
        /// </summary>
        /// <param name="format"></param>
        /// <param name="list"></param>
        public SolDevException(string format, params object [] list ) : this ()
        {
            SolDevLogger.Error(format, list);
            MessageBox.Show(String.Format(format, list), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        
        #endregion
    }


   public enum SolDevExceptionKey
    {
        #region Editor exceptions
        /// <summary>
        ///     An exception that should not appear
        /// </summary>
        appGenericError = 0x0001,

        /// <summary>
        ///     State machine exceptions (sm starting with 0x0010).  
        /// </summary>
        smGenericError = 0x0010,

        /// <summary>
        ///     Initial state has not been defined. The state is need
        /// to define a start point and to separate language words
        /// </summary>
        smCannotFindInitialState,

        /// <summary>
        ///     Invalid symbol has been encountered. There is no defined
        /// state to transact to using given symbol.
        /// </summary>
        smInvalidSymbol,

        /// <summary>
        ///     The state has not been defined (should not appear if
        /// state machine has been validate before usage
        /// </summary>
        smStateNotDefined,

        /// <summary>
        ///     not all states were define
        /// </summary>
        smIncompleteStateMachine,

        /// <summary>
        ///     There are at least two states to which state machine
        ///  can perform transaction 
        /// </summary>
        smNonDeterministic,

        /// <summary>
        ///     The state machine could not find a valid transaction for
        /// this symbol
        /// </summary>
        smNoNextStransaction,
        #endregion

        #region Application errors
        /// <summary>
        ///      The user login is empty
        /// </summary>
        UserLoginCannotBeEmpty,
        /// <summary>
        ///      Unable to load settings from application data folder
        /// </summary>
        /// 
        CannotLoadSettings,
        /// <summary>
        ///      Unable to save settings to application data folder
        /// </summary>
        CannotSaveSettings,
        #endregion
    }
}
